﻿using Kinlo.Entitys;
using Kinlo.MESDocking;
using MaterialDesignThemes.Wpf;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using UIWindows.Enums;
using UIWindows.GlobalStatics;
using UIWindows.Models.ParameterModels;
using UIWindows.Tools.Helper;
using XCode;
using XingLucifer.IBase.Enums;

namespace UIWindows.ViewModels.Dialogs
{
    public class MaterialDialogViewModel : UIWindows.Domains.ViewModelBase
    {
        public Visibility IsInShow { get; set; }
        public Visibility IsOutShow { get; set; }
        public string MaterialBarcode { get; set; } = string.Empty;
        public string MaterialCount { get; set; } = string.Empty;
        Dictionary<string, object> param;
        IMESInteraction _interactionMaterialIn;
        IMESInteraction _interactionMaterialOut;
        ParameterStatic _parameterStatic;
        CollectionItemsParameterModel _collectionItems;
        public MaterialDialogViewModel(bool isIn, IMESInteraction interactionMaterialIn, IMESInteraction interactionMaterialOut,
            ParameterStatic parameterStatic, CollectionItemsParameterModel collectionItems)
        {
            IsInShow = isIn ? Visibility.Visible : Visibility.Collapsed;
            IsOutShow = isIn ? Visibility.Collapsed : Visibility.Visible;
            _interactionMaterialIn = interactionMaterialIn;
            _interactionMaterialOut = interactionMaterialOut;
            _parameterStatic = parameterStatic;
            _collectionItems = collectionItems;
            //mes报文
            param = new Dictionary<string, object>
            {
                { "machineNo", _parameterStatic.CollectionItemsParameter.EquipNum },
                { "producttype", _parameterStatic.CollectionItemsParameter.ProductType.ToString() },

                { "parameter", new Dictionary<string, object>()
                    {
                        { "tenantID", _parameterStatic.CollectionItemsParameter.ProductionLineNo },//产线编号
                        { "technicsProcessCode", _parameterStatic.CollectionItemsParameter.TechnicsProcessCode },//工序编码
                        { "equipCode", _parameterStatic.CollectionItemsParameter.EquipNum },//设备编号                                                              
                        { "uploadTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},//上卸料时间                                                             
                        { "productCodeList", new List<Dictionary<string, string>>
                           {
                               new Dictionary<string, string>
                               {
                                    { "productCode","" },//上卸料物料条码
                                    { "productCount","" },// 上卸料物料数量
                               }
                           }
                        }
                    }
                },
            };

            if (isIn)
            {
                var parameter = (Dictionary<string, object>)param["parameter"];
                parameter.Add("produceOrderCode", _parameterStatic.CollectionItemsParameter.WorkOrder);//工单编码
            }

        }

        public SnackbarMessageQueue MyMessageQueue { get; set; } = new SnackbarMessageQueue(new TimeSpan(0, 0, 3));
        public Commands.DelegateCommand MaterialInCMD => new Commands.DelegateCommand(() =>
        {
            //if (string.IsNullOrEmpty(MaterialBarcode) || !double.TryParse(MaterialCount, out var _))
            //{
            //    MyMessageQueue.Enqueue("条码为空或数量格式不正确！", "知道啦", () => { });
            //    return;
            //}
            //var parameter = param["parameter"] as Dictionary<string, object>;
            //parameter["uploadTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//上卸料时间     
            //((List<Dictionary<string, string>>)parameter["productCodeList"])[0]["productCode"] = MaterialBarcode;//上卸料物料条码   
            //((List<Dictionary<string, string>>)parameter["productCodeList"])[0]["productCount"] = MaterialCount;// 上卸料物料数量     
            //var resultTypes = _interactionMaterialIn.Interaction(_parameterStatic.MESParameter.BaseUrl + _parameterStatic.MESParameter.MaterialInRoute,
            //     _parameterStatic.MESParameter.MaterialInTime,
            //     _parameterStatic.MESParameter.MaterialInNumber,
            //     DateTime.Now, new ProductionData[] { new ProductionData() }, param);

            //if (resultTypes == ResultTypes.合格)
            //{
            //    _collectionItems.BatchElectrolyte = _parameterStatic.CollectionItemsParameter.BatchElectrolyte = MaterialBarcode;
            //    _collectionItems.BatchCount = _parameterStatic.CollectionItemsParameter.BatchCount = MaterialCount;
            //    _ = $"[原材料上料] 成功！".MessageDialogs(MessageLevelType.成功, true, PopUpTypes.Global);
            //    if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
            //}
        });
        public Commands.DelegateCommand MaterialOutCMD => new Commands.DelegateCommand(() =>
        {
            //double materialCount = 0;
            //if (string.IsNullOrEmpty(MaterialBarcode) || !double.TryParse(MaterialCount, out materialCount))
            //{
            //    MyMessageQueue.Enqueue("条码为空或数量格式不正确！", "知道啦", () => { });
            //    return;
            //}
            //var parameter = param["parameter"] as Dictionary<string, object>;
            //parameter["uploadTime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//上卸料时间     
            //((List<Dictionary<string, string>>)parameter["productCodeList"])[0]["productCode"] = MaterialBarcode;//上卸料物料条码   
            //((List<Dictionary<string, string>>)parameter["productCodeList"])[0]["productCount"] = MaterialCount;// 上卸料物料数量     
            //var resultTypes = _interactionMaterialOut.Interaction(_parameterStatic.MESParameter.BaseUrl + _parameterStatic.MESParameter.MaterialOutRoute,
            //     _parameterStatic.MESParameter.MaterialOutTime,
            //     _parameterStatic.MESParameter.MaterialOutNumber,
            //     DateTime.Now, new ProductionData[] { new ProductionData() }, param);
            //if (resultTypes == ResultTypes.合格)
            //{
            //    double residue = 0;
            //    if (double.TryParse(_parameterStatic.CollectionItemsParameter.BatchCount, out var batchCount))
            //        residue = batchCount - materialCount;
            //    if (residue <= 0)
            //    {
            //        _collectionItems.BatchElectrolyte = _parameterStatic.CollectionItemsParameter.BatchElectrolyte = string.Empty;
            //        _collectionItems.BatchCount = _parameterStatic.CollectionItemsParameter.BatchCount = "0";
            //    }
            //    else
            //    {
            //        _collectionItems.BatchCount = _parameterStatic.CollectionItemsParameter.BatchCount = residue.ToString();
            //    }
            //    _ = $"[原材料卸料] 成功！".MessageDialogs(MessageLevelType.成功, true, PopUpTypes.Global);
            //    if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
            //}
        });
        public Commands.DelegateCommand CloseCMD => new Commands.DelegateCommand(() =>
        {
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }

        });
    }
}
